package com.ruoyi.library.user.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.library.domain.NexusToolLibrary;
import com.ruoyi.library.domain.dto.NexusToolLibraryDTO;
import com.ruoyi.library.domain.vo.NexusToolLibraryVo;
import com.ruoyi.library.service.INexusToolLibraryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源库Controller
 *
 * @author Afanda
 * @date 2025-04-29
 */
@RestController
@Slf4j
@RequestMapping("/User/NexusSphere/library")
public class NexusToolLibraryUserController extends BaseController {
    @Autowired
    private INexusToolLibraryService nexusToolLibraryService;

    @Autowired
    private RedisCache redisCache;

    private static final String LIBRARY_CACHE_KEY = "library";


    @GetMapping("/list")
    public AjaxResult selectNexusToolLibraryVoListByCategoryId(Long id) {

        log.info("根据首页分类id:{}获取数据", id);
        if (id == null) {
            return AjaxResult.error("id为空");
        }
        String key = LIBRARY_CACHE_KEY + id;
        Object cacheObject = redisCache.getCacheObject(key);
        List<NexusToolLibraryVo> nexusToolLibraryVos = null;
        if(cacheObject == null){
            log.info("缓存中不存在数据，从数据库中获取");
            nexusToolLibraryVos = nexusToolLibraryService.selectNexusToolLibraryVoListByCategoryId(id);
            if (nexusToolLibraryVos == null) {
                return AjaxResult.error("获取数据失败，请重新尝试");
            }
            redisCache.setCacheObject(key, nexusToolLibraryVos);
        }else{
            nexusToolLibraryVos = (List<NexusToolLibraryVo>) cacheObject;
            log.info("缓存中存在数据",nexusToolLibraryVos);
        }

        return AjaxResult.success(nexusToolLibraryVos);
    }

    @GetMapping("/nav/list")
    public AjaxResult selectNexusToolLibraryVoListByNavId(Long id) {
        log.info("根据导航栏id:{}获取数据", id);
        if (id == null) {
            return AjaxResult.error("id为空");
        }

        List<NexusToolLibraryVo> nexusToolLibraryVos = nexusToolLibraryService.selectNexusToolLibraryVoListByNavId(id);
            if (nexusToolLibraryVos == null) {
                return AjaxResult.error("获取数据失败，请重新尝试");
            }

        return AjaxResult.success(nexusToolLibraryVos);
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (id == null) {
            return AjaxResult.error("id为空");
        }
        NexusToolLibrary nexusToolLibrary = nexusToolLibraryService.selectNexusToolLibraryById(id);
        return AjaxResult.success(nexusToolLibrary);
    }

    @GetMapping("/search/toolLibrary")
    public AjaxResult searchToolLibrary(NexusToolLibraryDTO nexusToolLibrary){
        log.info("资源库获取参数：{}",nexusToolLibrary);
        if (nexusToolLibrary == null) {
            return AjaxResult.error("参数为空");
        }
        List<NexusToolLibraryVo> nexusToolLibraryList = nexusToolLibraryService.searchToolLibrary(nexusToolLibrary);
        return AjaxResult.success(nexusToolLibraryList);
    }

}


